﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;




//using System.
//using System.Me

namespace Model
{
    public class Klient
    {
        IPEndPoint serveradr;
        NetworkStream networkstream;
        StreamReader streamreader;
        StreamWriter streamwriter;

        private string ip;
        private int port;
        Socket server;
        bool running;
        Thread lytServer;

        public event BeskedDelegate ChatModtagetEvent;

        public Klient(int port, string ip)
        {
            this.running = true;
            this.serveradr = new IPEndPoint(IPAddress.Parse(ip), port);
            this.server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.ip = ip;
            this.port = port;
        }

        public void Forbind()
        {
            while (running) // Forbindelsen fortsætter indtil 
            {
                try
                {


                    server.Connect(serveradr); //Forbind socket til server
                    networkstream = new NetworkStream(server);	    // Lav networkstream til server
                    streamreader = new StreamReader(networkstream);	// Lav streamreader til server
                    streamwriter = new StreamWriter(networkstream);
                    lytServer = new Thread(LytTilServer);
                    lytServer.Start();

                }
                catch (SocketException e)
                {
                    e.ToString();
                    return;
                }


            }
            streamreader.Close();
            streamwriter.Close();
            networkstream.Close();
            server.Shutdown(SocketShutdown.Both);
            server.Close();
            {
            }

        }

        public void Stop()
        {
            try
            {
                streamwriter.WriteLine("Farvel Server");
                streamwriter.Flush();
                running = false; // angiver at forbindelsen skal stoppe;
            }
            catch (Exception)
            {
                
                //throw;
            }

        }
        public void Byd(int bud)
        {
            streamwriter.WriteLine("Bud:");
            //streamwriter.WriteLine("Bud: " + bud.ToString());
            streamwriter.Flush();
        }
        public void LytTilServer()
        {
            while (true)
            {
                string input = streamreader.ReadLine();
                if (input != null)
                {
                    SkrivTilGui(input);
                }
            }
                    }
        public void SkrivTilGui(string streamReaderObj)
        {
            //

            string chat = streamReaderObj;
            //StreamReader streamReader = (StreamReader)streamReaderObj;

                ChatModtagetEvent(chat);

        }
    }
}